/**
  ******************************************************************************
  * @file    RS232_DRIVER.h
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   USART operation
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 

#ifndef  __RS232_DRIVER_H__
#define  __RS232_DRIVER_H__

#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief GLOBALS RAM definition
  */
#ifdef   RS232_DRIVER_GLOBALS
#define  RS232_DRIVER_EXT
#else
#define  RS232_DRIVER_EXT  extern
#endif


RS232_DRIVER_EXT uint8_t RS232RXWorkMode;
RS232_DRIVER_EXT uint8_t RS232RXOverTiCNT;



#define RS232TIM_SetCompare                   TIM_SetCompare1
#define RS232TXDMA_Channel_IRQHandler         DMA1_Channel7_IRQHandler
#define RS232RXDMA_Channel_IRQHandler         DMA1_Channel6_IRQHandler
#define RS232USART_IRQHandler                 USART2_IRQHandler


/* USART Cfg */
#define APP_RS232_CLK1                        RCC_APB1Periph_USART2
#define APP_RS232_GPIO_CLK                    (RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC)

#define APP_RS232_RTXGPIO                     GPIOD
#define APP_RS232_RXPin                       GPIO_Pin_6
#define APP_RS232_TXPin                       GPIO_Pin_5
#define APP_RS485_CONGPIO                     GPIOC
#define APP_RS485_CONPin                      GPIO_Pin_13

#define APP_RS232                             USART2
#define APP_RS232_BaudRate                    9600
#define APP_RS232_WordLength                  USART_WordLength_8b
#define APP_RS232_StopBits                    USART_StopBits_2
#define APP_RS232_Parity                      USART_Parity_No
#define APP_RS232_HardwareFlowControl         USART_HardwareFlowControl_None
#define APP_RS232_Mode                        (USART_Mode_Rx | USART_Mode_Tx)

#define APP_RS232_IRQChannel                  USART2_IRQn
#define APP_RS232_IRQPrePrio                  0x0E
#define APP_RS232_IRQSubPrio                  0x0E
#define APP_RS232_RX_ITx                      USART_IT_RXNE
#define APP_RS232_RX_FLAGx                    USART_FLAG_RXNE
#define APP_RS232_TX_ITx                      USART_IT_TC
#define APP_RS232_TX_FLAGx                    USART_FLAG_TC

#define APP_RS232_TXE_ITx                     USART_IT_TXE
#define APP_RS232_TXE_FLAGx                   USART_FLAG_TXE


/* DMA Cfg */
#define RS232RX_DR_Address                    ((uint32_t)0x40004404)
#define RS232TX_DR_Address                    ((uint32_t)0x40004404)
                                              
#define RS232DMA_RCCPeriph                    RCC_AHBPeriph_DMA1
#define RS232RXDMA_IRQChannel                 DMA1_Channel6_IRQn
#define RS232RXDMA_IRQPrePrio                 0x0E
#define RS232RXDMA_IRQSubPrio                 0x0F
                                              
#define RS232RXDMA_Channel                    DMA1_Channel6
#define RS232RXDMA_Channel_IT                 DMA_IT_TC
#define RS232RXDMA_IT_TCMode                  DMA1_IT_TC6
#define RS232RXDMA_IT_GLMode                  DMA1_IT_GL6
#define RS232RXDMA_FLAG_Mode                  DMA1_FLAG_TC6
                                              
#define RS232RXDMA_PeripheralBaseAddr         ((uint32_t)RS232RX_DR_Address)      
#define RS232RXDMA_MemoryBaseAddr             ((uint32_t)RS232RXBufferData)
#define RS232RXDMA_DIR                        DMA_DIR_PeripheralSRC            
#define RS232RXDMA_PeripheralInc              DMA_PeripheralInc_Disable        
#define RS232RXDMA_MemoryInc                  DMA_MemoryInc_Enable             
#define RS232RXDMA_PeripheralDataSize         DMA_PeripheralDataSize_Byte  
#define RS232RXDMA_MemoryDataSize             DMA_MemoryDataSize_Byte      
#define RS232RXDMA_Mode                       DMA_Mode_Normal                  
#define RS232RXDMA_Priority                   DMA_Priority_High                
#define RS232RXDMA_M2M                        DMA_M2M_Disable                  
                                              
#define RS232TXDMA_IRQChannel                 DMA1_Channel7_IRQn
#define RS232TXDMA_IRQPrePrio                 0x0E
#define RS232TXDMA_IRQSubPrio                 0x0F
                                              
#define RS232TXDMA_Channel                    DMA1_Channel7
#define RS232TXDMA_Channel_IT                 DMA_IT_TC
#define RS232TXDMA_IT_TCMode                  DMA1_IT_TC7
#define RS232TXDMA_IT_GLMode                  DMA1_IT_GL7
#define RS232TXDMA_FLAG_Mode                  DMA1_FLAG_TC7
                                              
#define RS232TXDMA_PeripheralBaseAddr         ((uint32_t)RS232TX_DR_Address)  
#define RS232TXDMA_MemoryBaseAddr             ((uint32_t)RS232TXBufferData)
#define RS232TXDMA_DIR                        DMA_DIR_PeripheralDST                                         
#define RS232TXDMA_PeripheralInc              DMA_PeripheralInc_Disable       
#define RS232TXDMA_MemoryInc                  DMA_MemoryInc_Enable            
#define RS232TXDMA_PeripheralDataSize         DMA_PeripheralDataSize_Byte 
#define RS232TXDMA_MemoryDataSize             DMA_MemoryDataSize_Byte     
#define RS232TXDMA_Mode                       DMA_Mode_Normal                 
#define RS232TXDMA_Priority                   DMA_Priority_High               
#define RS232TXDMA_M2M                        DMA_M2M_Disable                 



RS232_DRIVER_EXT void RS232DateSendCOMM_handling(uint8_t *Point,uint16_t DataNumber);
RS232_DRIVER_EXT void DMA_RS232RXConfig(uint32_t RS232RXDMA_MemoryAdr,uint16_t RS232RXDMA_Number);
RS232_DRIVER_EXT void PORInit_RS232Interface(void);

RS232_DRIVER_EXT void RS232USART_IRQHandler(void);
RS232_DRIVER_EXT void RS232RXDMA_Channel_IRQHandler(void);
RS232_DRIVER_EXT void RS232TXDMA_Channel_IRQHandler(void);

RS232_DRIVER_EXT void NVIC_RS232Config(FunctionalState TIMCtrSta);

#ifdef __cplusplus
}
#endif

#endif	/*	__RS232_DRIVER_H__	*/
